<?php
namespace app\weapp\controller;

use app\common\controller\Weapp;
use app\weapp\library\WeAppApi;
use EasyWeChat\Work\Application;
use app\common\model\Member;
use think\facade\Db;
use think\facade\Log;
use app\weapp\model\Activity as ActivityModel;
use app\common\library\ImageBind;
class Activity extends Weapp
{
    /**
     * 无需登录的方法
     * 访问本控制器的此方法，无需会员登录
     * @var array
     */
    protected array $noNeedLogin = ['auth','detail','qrcode'];

    /**
     * 无需鉴权的方法
     * @var array
     */
    protected array $noNeedPermission = ['index','detail'];
    public function initialize():void
    {
        parent::initialize();
    }
    /**
     * 获取活动列表
     */
    public function index(){
        $page = $this->request->param('page',1);
        $pageSize = $this->request->param('page_size',20);
        $list = ActivityModel::where('status',1)->page($page,$pageSize)->select();
        $this->success('',$list);
    }
    /**
     * 获取活动详情
     */
    public function detail(){
        
        $id = $this->request->param('id','');
        $info = ActivityModel::find($id);
        $this->success('',$info);
    }
    public function qrcode(){
        $id = $this->request->param('id','');
        ImageBind::bulidImage($id);
        return $this->success('',$this->request->domain().'/static/images/special/'.$id.'.jpg');
    }

    public function auth(){
        $agent_id = $this->request->param('agent_id','');
        $config = [
            'corp_id' => 'ww5d7b19c3c1ce8e1f',
            'secret' => 'pOBrwunxH-2otSIwR_6FKQjhXn0XAEiKX0z_q93u1xE',
            
        ];
        $app = new Application($config);
        $utils = $app->getUtils();
        if(empty($agent_id)){
            $config = $utils->buildJsSdkConfig('https://wework.sdlycwzx.com/notice/',['hideOptionMenu','createWWLoginPanel'],[]);
            $data['timestamp'] = $config['timestamp'];
            $data['nonceStr'] = $config['nonceStr'];
            $data['signature'] = $config['signature'];
        }else{
            $data = $utils->buildJsSdkAgentConfig($agent_id,'https://wework.sdlycwzx.com/notice/',['hideOptionMenu','createWWLoginPanel'], []);
        }
        
        //{ timestamp, nonceStr, signature }
        $this->success('',$data);
    }
}